package com.application.configuration;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.application.mybatis.UserMybatis;
@Configuration
public class MyUserDetailService implements UserDetailsService{
	@Autowired
	UserMybatis mb;
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		System.err.println(username);
		String password  = mb.selectByName(username);
		if(password==null){
			throw new UsernameNotFoundException("用户不存在！");
		}
		
		// TODO Auto-generated method stub
		System.err.println(username+"======"+password);
		List<SimpleGrantedAuthority> l = new ArrayList<SimpleGrantedAuthority>();
		Long id = mb.selectRoleByUserName(username);
		for(String auth:mb.selectAuth(id)){
			SimpleGrantedAuthority sga = new SimpleGrantedAuthority(auth);
			System.err.println(auth);
			l.add(sga);
		};
		
		return new User(username, password, l);
	}

}
